<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-collapse v-model="activeKey">
          <!-- 基本信息 -->
          <a-collapse-panel key="1" header="基本信息">
            <a-row :gutter="48">
              <a-col :span="8">
                <a-form-model-item label="申请人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createBy">
                  <select-user-by-dep disabled :multi="false" v-model="model.createBy"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="申请时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
                  <j-date
                    placeholder="请选择申请时间"
                    v-model="model.createTime"
                    :show-time="true"
                    date-format="YYYY-MM-DD HH:mm:ss"
                    style="width: 100%"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="会议室名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="roomName">
                  <a-input :disabled="formDisabled" v-model="model.roomName" :maxLength="200" placeholder="请输入会议室名称"></a-input>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="48">
              <a-col :span="8">
                <a-form-model-item label="所在位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="location">
                  <a-input :disabled="formDisabled" v-model="model.location" :maxLength="200" placeholder="请输入所在位置"></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="所属公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="company">
                  <j-select-depart :disabled="formDisabled" v-model="model.company" placeholder="请选择所属公司"></j-select-depart>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="管理员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="administrator">
                  <select-user-by-dep :disabled="formDisabled" :multi="false" v-model="model.administrator"/>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="48">
              <a-col :span="8">
                <a-form-model-item label="容纳人数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="capacity">
                  <a-input-number :disabled="formDisabled" v-model="model.capacity" :min="0" :max="99999" placeholder="请输入容纳人数" style="width: 100%"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="会议室状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="roomStatus">
                  <j-dict-select-tag :disabled="formDisabled" type="list" v-model="model.roomStatus" dictCode="meeting_room_status" placeholder="请选择会议室状态"/>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="48">
              <a-col :span="24">
                <a-form-model-item label="配套设备" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="equipment">
                  <a-textarea :disabled="formDisabled" v-model="model.equipment" rows="4" :maxLength="500" placeholder="请输入配套设备"/>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="48">
              <a-col :span="24">
                <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
                  <a-textarea :disabled="formDisabled" :max-length="500" v-model="model.remark" rows="4" placeholder="请输入备注"/>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="2" header="附件信息">
            <a-row :gutter="48">
              <a-col :span="24">
                <a-form-model-item label="附件信息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
                  <j-upload :disabled="formDisabled" :buttonVisible="!formDisabled" v-model="model.attachment"></j-upload>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
        </a-collapse>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction } from '@/api/manage'
import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
import moment from 'moment'

export default {
  name: 'ErpMeetingRoomForm',
  components: {
    SelectUserByDep
  },
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  watch: {
    activeKey(key) {
      this.$forceUpdate()
    }
  },
  data () {
    return {
      moment,
      model: {
        createTime: ''
      },
      activeKey: ['1', '2'],
      labelCol: {
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      confirmLoading: false,
      validatorRules: {
      roomName: [
          { required: true, message: '请输入会议室名称!', whitespace: true }
      ],
      location: [
          { required: true, message: '请输入所在位置!', whitespace: true }
      ],
      company: [
          { required: true, message: '请输入所属公司!' }
      ],
      administrator: [
          { required: true, message: '请输入管理员!' }
      ],
      capacity: [
          { required: true, message: '请输入容纳人数!' },
          { pattern: /^[0-9]*[1-9][0-9]*$/, message: '请输入正整数!' }
      ],
      roomStatus: [
          { required: true, message: '请选择会议室状态!' }
      ],
      equipment: [
          { required: true, message: '请输入配套设备!', whitespace: true }
      ]
      },
      url: {
        add: '/api/erp/oa/meeting/erpMeetingRoom/add',
        edit: '/api/erp/oa/meeting/erpMeetingRoom/edit',
        queryById: '/api/erp/oa/meeting/erpMeetingRoom/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
    userInfo() {
      return this.$store.getters.userInfo
    }
  },
  created () {
     // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add () {
      this.edit(this.modelDefault);
    },
    edit (record) {
      this.$refs.form.resetFields();
      this.model = Object.assign({}, record);
      if (!this.model.id) {
        this.$set(this.model, 'createBy', this.userInfo.empId)
        this.$set(this.model, 'createTime', moment(new Date()).format('YYYY-MM-DD HH:mm:ss'))
      }
      this.visible = true;
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
             method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }
      })
    }
  }
}
</script>
